const JwtStrategy = require('passport-jwt').Strategy,//验证jwt
    ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');//加密密码的插件
const User = mongoose.model('users');
const keys = require('./keys');
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrkey;

module.exports = passport =>{
    passport.use(new JwtStrategy(opts, (jwt_payload, done) =>{
        // console.log(jwt_payload);
        User.findById(jwt_payload.id)
            .then((user) =>{
                if (user) {
                    return done(null, user);
                }
                return done(null, false);
            })
            .catch(err => {
                console.log(err)
            })
    }));
}